package client;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import service.IService;

public class CaseTest extends AbstractTest implements IService{
	
    public static void main(String args[]) throws Exception {
        
    	//testRequestJsonGetCases();
    	//testRequestJsonPutCase();
    	//testRequestJsonGetCase();
    	//testRequestJsonGetCasesTree();
    	//testRequestJsonRemoveCase();
    	//testRequestJsonGetCasesTree();
    	testRequestJsonRunCase(1L);
    	//testRequestJsonRunCase(23L);
        System.out.println("\n"); 
        System.exit(0);
    }
    private static void testRequestJsonGetCase()throws Exception{
    	System.out.println("\n");
        System.out.println("Sent HTTP GET request to get single case info, expect JSON");
        String url=getTestHost()+ACTION_CASE+"/1";
        System.out.println("Request URL "+url);
        GetMethod get = new GetMethod(url);
        get.addRequestHeader("Accept" , "application/json");
        HttpClient httpclient = new HttpClient();
       
        try {
            int result = httpclient.executeMethod(get); 
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            System.out.println(get.getResponseBodyAsString());
        } finally {
            get.releaseConnection();             
        }
    }
    private static void testRequestJsonGetCases()throws Exception{
    	System.out.println("\n");
        System.out.println("Sent HTTP GET request to get cases info, expect JSON");
        GetMethod get = new GetMethod(getTestHost()+ACTION_CASES+"/1");
        get.addRequestHeader("Accept" , "application/json");
        HttpClient httpclient = new HttpClient();
       
        try {
            int result = httpclient.executeMethod(get); 
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            System.out.println(get.getResponseBodyAsString());
        } finally {
            get.releaseConnection();             
        }
    }
    
    private static void testRequestJsonGetCasesTree()throws Exception{
    	System.out.println("\n");
        System.out.println("Sent HTTP GET request to get cases info, expect JSON");
        //String url="http://10.58.88.212:8080/BpcTesterWeb/bpccasestree";//
        String url=getTestHost()+ACTION_CASESTREE;
        System.out.println("Request URL "+url);
        GetMethod get = new GetMethod(url);
        
        get.addRequestHeader("Accept" , "application/json");
        HttpClient httpclient = new HttpClient();
       
        try {
            int result = httpclient.executeMethod(get); 
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            System.out.println(get.getResponseBodyAsString());
        } finally {
            get.releaseConnection();             
        }
    }
    
    private static void testRequestJsonPutCase()throws Exception{
    	System.out.println("\n");
        System.out.println("Sent HTTP PUT request to save case info, use JSON, expect RESPONSE");
        PutMethod post = new PutMethod(getTestHost()+ACTION_SAVECASE+"/0");
        post.addRequestHeader("Accept" , "application/json");
        HttpClient httpclient = new HttpClient();

        try {
        	
        	
        	//post.setRequestEntity(new StringRequestEntity(" {\"Case\":[{\"id\":0,\"name\":\"CASE1\",\"type\":1,\"catalogId\":1}]}"));
//        	String url="{\"Case\":{\"id\":\"\",\"name\":\"a11\",\"catalogId\":\"20\"" +
//        			",\"steps\":[{\"name\":\"step1\"}]" +
//        			",\"subcases\":[{\"name\":\"subcase1\"" +
//        			"	,\"params\":[{\"name\":\"nnn\",\"value\":\"vvv\"}]" +
//        			"}]}}";
        	
        	String url = "{\"Case\":{\"id\":0,\"name\":\"b01\",\"catalogId\":1,\"steps\":[{\"id\":1,\"name\":\"step1\",\"desc\":\"desc1\",\"url\":\"sap\",\"method\":\"\",\"format\":\"\",\"profile\":\"\",\"request\":\"123456\",\"response\":\"\",\"casedetail_id\":1}],\"subcases\":[{\"id\":1,\"name\":\"subcase1\"," +
        			"\"params\":[{\"name\":\"56\",\"value\":\"55566\"},{\"name\":\"aaa\",\"value\":\"\"},{\"name\":\"ef\",\"value\":\"\"}]" +
        			",\"casedetail_id\":1}]}}";
        	post.setRequestEntity(
        			new StringRequestEntity(        					
        					url
        					)
        			);
        	System.out.println(url);
        	
            int result = httpclient.executeMethod(post);
            
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            System.out.println(post.getResponseBodyAsString());
        } finally {
            post.releaseConnection();            
        }
    }
    @SuppressWarnings("unused")
    private static void testRequestJsonRemoveCase()throws Exception{
    	System.out.println("\n");
        System.out.println("Sent HTTP POST request to remove case info, use JSON, expect RESPONSE");
        DeleteMethod post = new DeleteMethod(getTestHost()+ACTION_REMOVECASE+"/2");
        post.addRequestHeader("Accept" , "application/json");
        HttpClient httpclient = new HttpClient();

        try {
        	
            int result = httpclient.executeMethod(post);
            
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            System.out.println(post.getResponseBodyAsString());
        } finally {
            post.releaseConnection();            
        }
    }
    @SuppressWarnings("unused")
    private static void testRequestJsonRunCase()throws Exception{
    	testRequestJsonRunCase(1L);
    }
    @SuppressWarnings("unused")
    private static void testRequestJsonRunCase(Long id)throws Exception{
    	System.out.println("\n");
        System.out.println("Sent HTTP POST request to run case info, use JSON, expect RESPONSE");
        PostMethod post = new PostMethod(getTestHost()+ACTION_TESTSRUN+"/1");
        
        System.out.println(getTestHost()+ACTION_TESTSRUN+"/"+id);
        
        post.addRequestHeader("Accept" , "application/json");
        HttpClient httpclient = new HttpClient();

        try {
        	
            int result = httpclient.executeMethod(post);
            
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            System.out.println(post.getResponseBodyAsString());
            
        } finally {
            post.releaseConnection();            
        }
    }
}
